package SocketDemo.UDP;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author:周鹏
 * @date: 2020/11/9 17:13
 * @description:
 */
public class UdpServer {
    public static void main(String[] args) throws Exception {
        //开放端口
        DatagramSocket datagramSocket = new DatagramSocket(9090);
        //接收数据包
        //缓冲区
        //实现循环接收数据
        while (true) {
            byte[] buffer = new byte[1024];
            DatagramPacket dp = new DatagramPacket(buffer, 0, buffer.length);
            datagramSocket.receive(dp);
            //将包内容转换为字符串
            byte[] buffer1 = dp.getData();
            String datas = new String(buffer1, 0, buffer1.length);
            System.out.println(datas);
            if ("bye".trim().equals(datas)) {
                break;
            }
        }

        //关闭流
        datagramSocket.close();
    }
}

